package tree;

/**
 * @author : Rookie
 * @description : 二叉搜索树最小绝对差
 * @date : 2023/10/28
 */
public class BinarySearchTreeMinSub {

    public static void main(String[] args) {
        int[] tree = new int[] {8,4,10,1,7,-1,15};
        TreeNode root = BinaryTreeUtil.buildTree(tree);
        int result = getMinSub(null, root, Integer.MAX_VALUE);
        System.out.println("最小绝对差的值为：" + result);
    }

    private static int getMinSub(TreeNode prev,TreeNode curr,int result) {
        if (curr == null) {
            return result;
        }
        // 左
        result = getMinSub(prev,curr.left,result);
        // 中
        if (prev != null) {
            result = Math.min(curr.element - prev.element,result);
        }
        // 移动prev指针
        prev = curr;
        // 右
        result = getMinSub(prev,curr.right,result);
        return result;
    }
}
